<?php
/**
 * 向paypal付款  		
 * 
 */
class Helper_Pay_paypal extends Helper_Pay_Abstract
{
	function __construct(){
		// config 
		$this->__setconfig(array(
    		"posturl"=>"https://www.paypal.com/cgi-bin/webscr",
    		"sandbox_posturl"=>"https://www.sandbox.paypal.com/cgi-bin/webscr",
    		"cmd"=>"_xclick",
    		"url_cancel_return"=>"", //取消 返回冲值页
    		"url_return"=>"", //成功 返回到查询余额
    		'notify_url'=>'http://www.ibay365.com/index.php?controller=service&action=Paypalnotify',
    		//url ipn 是在 paypal 帐户 中设置地  http://www.coomao.com/index.php?controller=Services&action=ChargePaypalIPN
    	));
	}
	/***
	 * 产生向 paypal 付款的 链接 
	 */
	function getRequestUrl($setdata,$is_sanbox=0){
	   if($is_sanbox){
          $this->config["posturl"]=$this->config["sandbox_posturl"];
       }
	   $parameter=array(
			"business"=>$this->config["business"],
			"cmd"=>$this->config["cmd"],
			"item_name"=>date("Y-m-d H:i:s").". You pay to ".$this->config["business"],
			"item_number"=>0,
			"invoice"=>0,
			"amount"=>0,
			"currency_code"=>"USD",
			"cancel_return"=>$this->config["url_cancel_return"], //取消 返回冲值页
			"return"=>$this->config["url_return"],//"http://www.coomao.com/users/moneylog", //成功 返回到查询余额
			'notify_url'=>$this->config["notify_url"], // IPN 通知 
		);
		if(is_array($setdata))
		foreach($setdata as $set_key=>$set_value){
			$parameter[$set_key]=$set_value;
		}
		//var_dump($this->buildUrl($this->config["posturl"],$dataArr));
		$url=$this->buildUrl($this->config["posturl"],$parameter);
        Helper_Tlog::tofile('paypal.log',$url);
		return $url;
	}

	/**
     * 验证Post请求，通常为 notify url
     *
     * @return string
     */
    function verifyPost ()
    {
        Helper_tlog::tofile('paypal.log',$_POST);
        // 取 order 
        $order_id=$this->parseInvoice($_POST['invoice']);
        $order=Orders::find('order_id=?',$order_id)->getOne();
        // 未找到 order 退出 
        if($order->isNewRecord()) return false;
        $store_payment=Store_Payment::find('store_id=? And [payment.code]=?',$order->store_id,'paypal')->getOne();
    	$is_sanbox=$store_payment->status&2;
        $paypalIPN=new Helper_interface_pay_paypalIPN();
    	$return=$paypalIPN->send($_POST,$is_sanbox);
    	if($return=="VERIFIED"){
			//接收的收款状态为 已经收到款
			switch($_POST["payment_status"]){
                case 'Completed':
                    $payment_status='complete';
                    break;
                case 'Processed':
                default:
                    $payment_status='progress';
                    break;
            }
			//2,更新金额为已经收到
			$r=Orders::payed($order_id,$_POST["mc_gross"],$_POST["mc_currency"],$payment_status,'paypal');
            if($r){
                $assign['msg']='sussess';
            }else{
                $assign['msg']='fail';
            }
		}elseif($return=="INVALID"){
			$assign['msg']='INVALID fail';
		}
		Helper_tlog::tofile('paypal.log',$assign);
		//插入新记录
		//因为 paypal ,的状态可能有多种情况,这里暂时还无法处理 ，只能先记录下来.
		//用到的字段
		//var_dump($newLog_Paypalipn);
// 		$newLog_Paypalipn=array(
//             'data'=>$_POST,
//             'msg'=>$assign['msg']
//         );
// 		$Log_Paypalipn_new=new Log_Paypalipn($newLog_Paypalipn);
// 		$Log_Paypalipn_new->save(0, 'create');
    }
    function parseInvoice($invoice){
        list($order_id)=explode('-',$invoice);
        return $order_id;
    }
}